package pe.com.bbva.ge.vista.vaadin.ui;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import pe.com.bbva.ge.domain.seguridad.Usuario;
import pe.com.bbva.ge.negocio.ConfiguracionNegocio;
import pe.com.bbva.ge.negocio.SeguridadNegocio;
import pe.com.bbva.ge.utileria.Constante;
import pe.com.bbva.ge.vista.vaadin.ui.component.ButtonAccion;
import pe.com.bbva.ge.vista.vaadin.ui.utileria.exception.ValidacionException;

//import pe.com.bbva.reniec.dominio.Usuario;
//import pe.com.bbva.reniec.exception.ValidacionException;
//import pe.com.bbva.reniec.negocio.SeguridadService;
//import pe.com.bbva.reniec.utileria.Constante;
//import pe.com.bbva.reniec.utileria.Inject;

import com.vaadin.annotations.AutoGenerated;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.event.ShortcutListener;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;


public class LoginUI extends BaseUI implements ClickListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6584813009595092240L;

	@AutoGenerated
	private VerticalLayout mainLayout;

	@AutoGenerated
	private Panel pnlLogin;

	@AutoGenerated
	private VerticalLayout layoutLogin;

	@AutoGenerated
	private HorizontalLayout pnlBotonera;

	@AutoGenerated
	private ButtonAccion btnLogin;

	@AutoGenerated
	private GridLayout pnlDatos;

	@AutoGenerated
	private PasswordField txtClave;

	@AutoGenerated
	private Label lblClave;

	@AutoGenerated
	private TextField txtUsuario;

	@AutoGenerated
	private Label lblUsuario;

	@AutoGenerated
	private VerticalLayout pnlLogo;

	@AutoGenerated
	private Embedded imgLogin;

	@Autowired
	private ConfiguracionNegocio genericoNegocio;
	
	@Autowired
	private	SeguridadNegocio seguridadNegocio;
	
	public LoginUI() {		
		buildMainLayout();
		setCompositionRoot(mainLayout);		
	}
	
	@Override
	public void postConstruccion() {
		btnLogin.addListener((ClickListener)this);
		
		txtUsuario.addShortcutListener(new ShortcutListener("", KeyCode.ENTER, null){

			private static final long serialVersionUID = 1L;

			@Override
			public void handleAction(Object sender, Object target) {
				handleActionEnter();
			}
			
		});
		txtClave.addShortcutListener(new ShortcutListener("", KeyCode.ENTER, null){

			private static final long serialVersionUID = 1L;

			@Override
			public void handleAction(Object sender, Object target) {
				handleActionEnter();
			}
			
		});
		debugId();
	}
	
	private void debugId(){
		mainLayout.setDebugId("mainLayout");
		pnlLogin.setDebugId("pnlLogin");
		layoutLogin.setDebugId("layoutLogin");
		pnlBotonera.setDebugId("pnlBotonera");
		btnLogin.setDebugId("btnLogin");
		pnlDatos.setDebugId("pnlDatos");
		txtClave.setDebugId("txtClave");
		lblClave.setDebugId("lblClave");
		txtUsuario.setDebugId("txtUsuario");
		lblUsuario.setDebugId("lblUsuario");
		pnlLogo.setDebugId("pnlLogo");
		imgLogin.setDebugId("imgLogin");
	}
	
	private void handleActionEnter()
	{
		buttonClickLogin();
	}
	
	@Override
	public void buttonClick(ClickEvent event) {
		if(event.getButton().equals(btnLogin))
		{
			buttonClickLogin();
		}
	}
	
	@SuppressWarnings("deprecation")
	private void buttonClickLogin()
	{
		
		String username = txtUsuario.getValue().toString();
		String password = txtClave.getValue().toString();
        
        if(StringUtils.isBlank(username))
        {
        	throw new ValidacionException(Constante.CODIGO_MENSAJE.VALIDAR_TEXTBOX, new Object[]{"Usuario"});
        }
        
        if(StringUtils.isBlank(password))
        {
        	throw new ValidacionException(Constante.CODIGO_MENSAJE.VALIDAR_TEXTBOX, new Object[]{"Password"});
        }
        
        username = username.toUpperCase();
        
        Usuario usuario = seguridadNegocio.login(username, password);
        
        if(usuario != null)
        {
        	
			//Usuario usuario = genericoNegocio.obtener(1l, Usuario.class);
		
        	ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
    				.currentRequestAttributes();
    		HttpServletRequest request = requestAttributes.getRequest();
    		HttpSession session = request.getSession(true);
    		session.setAttribute(Constante.SESION.USUARIO, usuario);
    		Window windowReniec=getApplication().getMainWindow();
    		windowReniec.removeAllComponents();
    		windowReniec.setCaption("SGE");
    		PrincipalUI principalUI=new PrincipalUI();
    		windowReniec.addComponent(principalUI);
    		windowReniec.getContent().setHeight("100%");
    		windowReniec.getLayout().setMargin(false);
        }
	}

	@AutoGenerated
	private VerticalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("100%");
		mainLayout.setHeight("400px");
		mainLayout.setMargin(false);
		
		// top-level component properties
		setWidth("100.0%");
		setHeight("400px");
		
		// pnlLogin
		pnlLogin = buildPnlLogin();
		mainLayout.addComponent(pnlLogin);
		mainLayout.setComponentAlignment(pnlLogin, new Alignment(24));
		
		return mainLayout;
	}

	@AutoGenerated
	private Panel buildPnlLogin() {
		// common part: create layout
		pnlLogin = new Panel();
		pnlLogin.setImmediate(false);
		pnlLogin.setWidth("400px");
		pnlLogin.setHeight("300px");
		
		// layoutLogin
		layoutLogin = buildLayoutLogin();
		pnlLogin.setContent(layoutLogin);
		
		return pnlLogin;
	}

	@AutoGenerated
	private VerticalLayout buildLayoutLogin() {
		// common part: create layout
		layoutLogin = new VerticalLayout();
		layoutLogin.setImmediate(false);
		layoutLogin.setWidth("100.0%");
		layoutLogin.setHeight("100.0%");
		layoutLogin.setMargin(false);
		
		// pnlLogo
		pnlLogo = buildPnlLogo();
		layoutLogin.addComponent(pnlLogo);
		layoutLogin.setComponentAlignment(pnlLogo, new Alignment(24));
		
		// pnlDatos
		pnlDatos = buildPnlDatos();
		layoutLogin.addComponent(pnlDatos);
		layoutLogin.setComponentAlignment(pnlDatos, new Alignment(48));
		
		// pnlBotonera
		pnlBotonera = buildPnlBotonera();
		layoutLogin.addComponent(pnlBotonera);
		layoutLogin.setComponentAlignment(pnlBotonera, new Alignment(20));
		
		return layoutLogin;
	}

	@AutoGenerated
	private VerticalLayout buildPnlLogo() {
		// common part: create layout
		pnlLogo = new VerticalLayout();
		pnlLogo.setImmediate(false);
		pnlLogo.setWidth("350px");
		pnlLogo.setHeight("70px");
		pnlLogo.setMargin(false);
		
		// imgLogin
		imgLogin = new Embedded();
		imgLogin.setImmediate(false);
		imgLogin.setWidth("-1px");
		imgLogin.setHeight("-1px");
		imgLogin.setSource(new ThemeResource("img/bbvacontinental.gif"));
		imgLogin.setType(1);
		imgLogin.setMimeType("image/png");
		pnlLogo.addComponent(imgLogin);
		pnlLogo.setComponentAlignment(imgLogin, new Alignment(24));
		
		return pnlLogo;
	}

	@AutoGenerated
	private GridLayout buildPnlDatos() {
		// common part: create layout
		pnlDatos = new GridLayout();
		pnlDatos.setImmediate(false);
		pnlDatos.setWidth("350px");
		pnlDatos.setHeight("-1px");
		pnlDatos.setMargin(true);
		pnlDatos.setSpacing(true);
		pnlDatos.setColumns(2);
		pnlDatos.setRows(3);
		
		// lblUsuario
		lblUsuario = new Label();
		lblUsuario.setImmediate(false);
		lblUsuario.setWidth("-1px");
		lblUsuario.setHeight("-1px");
		lblUsuario.setValue("Usuario");
		pnlDatos.addComponent(lblUsuario, 0, 0);
		pnlDatos.setComponentAlignment(lblUsuario, new Alignment(34));
		
		// txtUsuario
		txtUsuario = new TextField();
		txtUsuario.setImmediate(false);
		txtUsuario.setWidth("-1px");
		txtUsuario.setHeight("-1px");
		pnlDatos.addComponent(txtUsuario, 1, 0);
		pnlDatos.setComponentAlignment(txtUsuario, new Alignment(33));
		
		// lblClave
		lblClave = new Label();
		lblClave.setImmediate(false);
		lblClave.setWidth("-1px");
		lblClave.setHeight("-1px");
		lblClave.setValue("Clave");
		pnlDatos.addComponent(lblClave, 0, 1);
		pnlDatos.setComponentAlignment(lblClave, new Alignment(34));
		
		// txtClave
		txtClave = new PasswordField();
		txtClave.setImmediate(false);
		txtClave.setWidth("-1px");
		txtClave.setHeight("-1px");
		pnlDatos.addComponent(txtClave, 1, 1);
		pnlDatos.setComponentAlignment(txtClave, new Alignment(33));
		
		return pnlDatos;
	}

	@AutoGenerated
	private HorizontalLayout buildPnlBotonera() {
		// common part: create layout
		pnlBotonera = new HorizontalLayout();
		pnlBotonera.setImmediate(false);
		pnlBotonera.setWidth("350px");
		pnlBotonera.setHeight("-1px");
		pnlBotonera.setMargin(false);
		
		// btnLogin
		btnLogin = new ButtonAccion();
		btnLogin.setCaption("Ingresar");
		btnLogin.setImmediate(true);
		btnLogin.setWidth("-1px");
		btnLogin.setHeight("-1px");
		pnlBotonera.addComponent(btnLogin);
		pnlBotonera.setComponentAlignment(btnLogin, new Alignment(6));
		
		return pnlBotonera;
	}

		

}
